# -*- coding: utf-8-*-
import random
import time
from selenium.webdriver import ActionChains
import schedule as schedule

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep


# 多选题的多个选项
def int_random(m, n, o):
    p = []
    while len(p) < o:
        new_int = random.randint(m, n)
        if (new_int not in p):
            p.append(new_int)
        else:
            pass
    return p

def get_track(distance):  # distance为传入的总距离
    # 移动轨迹
    track = []
    # 当前位移
    current = 0
    # 计算间隔
    t = 0.2
    # 初速度
    v = 0
    while current < distance:
        a = 50
        v0 = v
        # 当前速度
        v = v0 + a * t
        # 移动距离
        # move = v0 * t + 1 / 2 * a * t * t
        move = v0 * t + a * t
        # 当前位移
        current += move
        # 加入轨迹
        track.append(round(move))
    return track  # list 返回的是整个滑动条的多个焦点，可以模拟鼠标的缓慢滑动


def move_to_gap(slider, tracks):  # slider是要移动的滑块,tracks是要传入的移动轨迹
    ActionChains(driver).click_and_hold(slider).perform()
    for x in tracks:
        ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
    time.sleep(0.1)
    ActionChains(driver).release().perform()

service = Service('/usr/local/bin/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)

def run():
    global driver
    url = 'https://ks.wjx.top/vj/trZ7KvH.aspx'

    # 躲避智能检测
    option = webdriver.ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    option.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=option)
    driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                           {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
                            })

    try:
        driver.get(url)
        # 总共有13个题目
        i = 1
        while i <= 11:
            print("=========================")
            print(i)
            base_xpath1 = '//*[@id="divquestion{}"]'.format(i)
            base_xpath2 = base_xpath1 + '/ul/li'
            a = driver.find_elements_by_xpath(base_xpath2)

            if i == 1:
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 1)).click()
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 1)).send_keys('王**群')
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 2)).click()
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 2)).send_keys('滨*****工会')
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 3)).click()
                driver.find_element_by_css_selector('#divquestion{} > table > tbody > tr:nth-child({}) > td > div > textarea'.format(i, 3)).send_keys('185****5051')

            else:
                for r in range(len(a)):
                    print(i,r+1)
                    driver.find_element_by_css_selector('#divquestion{} > ul > li:nth-child({})'.format(i, r+1)).click()
            # #divquestion2 > ul > li:nth-child(1)
            i += 1
            # 点击提交按钮
        # time.sleep(0.5)
        # time.sleep(3) 设置为休眠3大概提交后显示10秒
        driver.find_element_by_xpath('//*[@id="submit_button"]').click()
        time.sleep(0.5)
        driver.find_element_by_css_selector('#alert_box > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) > button').click()
        time.sleep(0.5)
        driver.find_element_by_css_selector('#SM_BTN_WRAPPER_1').click()
        # time.sleep(1)
        # driver.find_element_by_xpath('//*[@id="submit_button"]').click()

        # # 出现点击验证码验证
        # time.sleep(1)
        # driver.find_element_by_xpath('//*[@id="alert_box"]/div[2]/div[2]/button').click()
        # time.sleep(0.5)
        # driver.find_element_by_xpath('//*[@id="submit_tip"]').click()
        # time.sleep(4)
        # huakuai = driver.find_element_by_css_selector('#nc_1_n1z')
        # move_to_gap(huakuai, get_track(328))
        # time.sleep(4)
    except:
        pass
    finally:
        # 关闭页面
        handles = driver.window_handles
        driver.switch_to.window(handles[0])
        time.sleep(30)
        # 刷新页面（可能不需要）
        # driver.refresh()
        # 关闭当前页面，如果只有一个页面，则也关闭浏览器
        driver.close()

if __name__=='__main__':
    run()